<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Installation script for
 *
 * @author No-CMS Module Generator
 */
class restapi extends CMS_REST_Controller {
	
	public function index_post(){
    	// do nothing
    }
	
	public function index_get(){
    	// do nothing
    }
	
	public function index_delete(){
    	// do nothing
    }
	
	public function index_put(){
    	// do nothing
    }
	
	public function login_post()
    {
		$data = new stdClass();
		
		$data->firstname = $this->post('firstname');
		$data->lastname = $this->post('lastname');
		$data->email = $this->post('email');
		$data->dob = $this->post('dob');
		$data->gender = $this->post('gender');
		$data->facebook_idn = $this->post('id');
        $data->profile_img = $this->post('profile_img');
		
		if ( empty($data->firstname) || empty($data->lastname) || empty($data->gender) || empty($data->facebook_idn) ) {
			$this->response(array('error' => 'Required params firstname, lastname, gender and id','errorCode'=>404), 200);
		}
        
        $this->load->library('User_lib');
        $user_info = $this->user_lib->save_user_login_data($data);
            
        if ( $user_info === FALSE ) {
			$this->response(array('error' => 'Failed to save data','errorCode'=>401), 200);
		} else {
            if ( is_object($user_info) ) {
                $this->response(array('response' => 'Success','responseCode'=>100,'data'=>array('user_id'=>$user_info->user_id,'returning_user'=>$user_info->returning_user)), 200);
            }
        }
		
		$data = null;
	}
    
    public function profile_get()
    {
		$data = new stdClass();
		
		$data->id = $this->get('user_id');
		$data->facebook_idn = $this->get('id');
		
		if ( empty($data->id) || empty($data->facebook_idn) ) {
			$this->response(array('error' => 'Required params user_id and id','errorCode'=>405), 200);
		}
	
        $this->load->library('User_lib');
		$user_profile_data = $this->user_lib->get_user_profile_data($data);
		
		if ( $user_profile_data === FALSE ) {
			$this->response(array('error' => 'Failed to get profile data','errorCode'=>402), 200);
		} else {
            $user_profile_data = (array) $user_profile_data;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$user_profile_data), 200);
		}
		
		$data = null;
    }
	
	public function update_profile_post()
    {
		$data = new stdClass();
		
		$data->firstname = $this->post('firstname');
		$data->lastname = $this->post('lastname');
		$data->email = $this->post('email');
		$data->dob = $this->post('dob');
		$data->gender = $this->post('gender');
		$data->facebook_idn = $this->post('id');
		$data->user_id = $this->post('user_id');
        $data->profile_img = $this->post('profile_img');
        $data->school_id = $this->post('school_id');
        $data->academic_year = $this->post('academic_year');
        $data->school_class = $this->post('school_class');
		
		if ( empty($data->firstname) || empty($data->lastname) || empty($data->gender) || empty($data->facebook_idn) ) {
			$this->response(array('error' => 'Required params firstname, lastname, gender and id','errorCode'=>404), 200);
		}
		
        $this->load->library('User_lib');
        $user_info = $this->user_lib->update_user_login_data($data);
		
		if ( $user_info === FALSE ) {
			$this->response(array('error' => 'Failed to save data','errorCode'=>401), 200);
		} else {
            if ( is_object($user_info) ) {
                $this->response(array('response' => 'Success','responseCode'=>100,'data'=>array('user_id'=>$user_info->user_id)), 200);
            }
        }
		
		$data = null;
    }
	
	public function update_profile_image_post()
    {
		$data = new stdClass();
		
		$data->facebook_idn = $this->post('id');
		$data->user_id = $this->post('user_id');
        $data->profile_img = $this->post('profile_img');
		
		if ( empty($data->facebook_idn) || empty($data->user_id) || empty($data->profile_img) ) {
			$this->response(array('error' => 'Required params id, user_id, profile_img','errorCode'=>406), 200);
		}
		
		$this->load->model('user_model');
		
		$user_info = $this->user_model->update_user_profile_image($data);
		
		if ( $user_info === FALSE ) {
			$this->response(array('error' => 'Failed to save data','errorCode'=>401), 200);
		} else {
			$this->response(array('response' => 'Success','responseCode'=>100), 200);
        }
		
		$data = null;
    }
    
    public function academic_year_get()
    {
        $this->load->model('user_model');
		$academic_year = $this->user_model->get_active_academic_year();
		
		if ( $academic_year === FALSE ) {
			$this->response(array('error' => 'Failed to get academic data','errorCode'=>402), 200);
		} else {
            $academic_year = (array) $academic_year;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$academic_year), 200);
		}
    }
    
    public function list_schools_get()
    {
		$data->school_state = (int) $this->get('school_state');
        $data->school_city = $this->get('school_city');
        $data->school_name = $this->get('school_name');
        
		if ( empty($data->school_state) ) {
			$this->response(array('error' => 'Required school state','errorCode'=>418), 200);
		}
        
        if ( empty($data->school_city) ) {
			$this->response(array('error' => 'Required school city','errorCode'=>419), 200);
		}
		
		$this->load->library('School_lib');
		$schools = $this->school_lib->get_active_schools($data);
		
		if ( $schools === FALSE ) {
			$this->response(array('error' => 'Failed to get schools data','errorCode'=>402), 200);
		} else {
            $schools = (array) $schools;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$schools), 200);
		}
    }
	
	public function list_pages_get()
    {
        $data = $this->_pagingParams();
        
        if ( empty($data->page_index) ) {
			$this->response(array('error' => 'Required page number','errorCode'=>407), 200);
		}
        
        $this->load->model('page_model');
		$pages = $this->page_model->get_active_pages($data);
		
		if ( $pages === FALSE ) {
			$this->response(array('error' => 'Failed to get pages data','errorCode'=>402), 200);
		} else {
            $pages = (array) $pages;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pages), 200);
		}
    }
    
	public function profile_post()
    {
        $this->response('do nothing', 200); // 200 being the HTTP response code
    }
	
    public function user_delete()
    {
    	$this->response('do nothing', 200); // 200 being the HTTP response code
    }
    
    private function _pagingParams()
    {
        $data = new stdClass();
        
        $data->page_index       = (int) $this->get('pageIndex');
        $data->page_size        = (int) $this->get('pageSize');
        $data->disable_paging   = (boolean) $this->get('disablePaging');
        
        if ( $data->disable_paging == FALSE && empty($data->page_index) ) {
			$this->response(array('error' => 'Please provide paging index or use disablePaging flag','errorCode'=>433), 200);
		} 
        
        return $data;
    }
    
    public function list_states_get()
    {
		$data = new stdClass();
        
		$data->school_state = $this->get('school_state');
		
		$this->load->model('school_model');
		$states = $this->school_model->get_states($data);
		
		if ( $states === FALSE ) {
			$this->response(array('error' => 'No state data','errorCode'=>416), 200);
		} else {
            $states = (array) $states;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$states), 200);
		}
    }
    
    public function list_cities_get()
    {
		$data = new stdClass();
        
		$data->school_state = (int) $this->get('school_state');
        $data->school_city = $this->get('school_city');
		
        if ( empty($data->school_state) ) {
			$this->response(array('error' => 'Required school state','errorCode'=>418), 200);
		}
        
		$this->load->model('school_model');
		$cities = $this->school_model->get_cities($data);
		
		if ( $cities === FALSE ) {
			$this->response(array('error' => 'No city data','errorCode'=>417), 200);
		} else {
            $cities = (array) $cities;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$cities), 200);
		}
    }
	
	// page manager begins from here
	
	public function upload_picture_post()
    {
        $data = new stdClass();
        
        $data->page_id = (int) $this->post('page_id');
        $data->user_id = (int) $this->post('user_id');
        $data->story = $this->post('story');
        $data->caption = $this->post('caption');
        
        if ( empty($data->page_id) ) {
			$this->response(array('error' => 'Required page id','errorCode'=>409), 200);
		}
        
        if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
        
        $this->config->load('scrapm');
        $scrapm_config = $this->config->item('scrapm');
        $this->load->library('upload', $scrapm_config['picture_config']);

        if ( $this->upload->do_upload('picture') ) {
            
            $uploaded_file_data = $this->upload->data();
            $data->uploaded_file_data = $uploaded_file_data;
            $this->load->library('Page_lib');
            $picture_details = $this->page_lib->upload_picture($data);    
        } else {
            $picture_details = FALSE;
        }
        
        if ( $picture_details === FALSE ) {
			$this->response(array('error' => 'Failed to upload picture','errorCode'=>420), 200);
		} else {
            $picture_details = (array) $picture_details;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$picture_details), 200);
		}
	}
    
    public function page_details_get()
    {
		$data = new stdClass();
        
        $data->page_id = (int) $this->get('pageID');
        $data->user_id = (int) $this->get('user_id');
        
		if ( empty($data->page_id) ) {
			$this->response(array('error' => 'Required page id','errorCode'=>409), 200);
		}
        
        if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
		
		$this->load->library('Page_lib');
		$pageDetails = $this->page_lib->get_page_details($data);
		
		if ( $pageDetails === FALSE ) {
			$this->response(array('error' => 'Failed to get page data','errorCode'=>410), 200);
		} else {
            $pageDetails = (array) $pageDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pageDetails), 200);
		}
    }
    
    public function user_pictures_get()
    {
		$data = $this->_pagingParams();
        
        $data->user_id = (int) $this->get('user_id');
        
		if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
		
		$this->load->library('Page_lib');
		$userDetails = $this->page_lib->get_user_picture_details($data);
		
		if ( $userDetails === FALSE ) {
			$this->response(array('error' => 'Failed to get user pictures data','errorCode'=>421), 200);
		} else {
            $userDetails = (array) $userDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$userDetails), 200);
		}
    }
    
    public function picture_comments_get()
    {
		$data = $this->_pagingParams();
        
        $data->id = (int) $this->get('picture_id');
        
		if ( empty($data->id) ) {
			$this->response(array('error' => 'Required picture id','errorCode'=>411), 200);
		}
		
		$this->load->model('page_model');
		$pictureComments = $this->page_model->get_picture_comments($data);
		
		if ( $pictureComments === FALSE ) {
			$this->response(array('error' => 'Failed to get pictures comments','errorCode'=>422), 200);
		} else {
            $pictureComments = (array) $pictureComments;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pictureComments), 200);
		}
    }
    
    public function picture_likes_get()
    {
		$data->id = (int) $this->get('picture_id');
        
		if ( empty($data->id) ) {
			$this->response(array('error' => 'Required picture id','errorCode'=>411), 200);
		}
		
		$this->load->model('page_model');
		$pictureLikes = $this->page_model->get_picture_likes($data);
		
		if ( $pictureLikes === FALSE ) {
			$this->response(array('error' => 'Failed to get pictures likes','errorCode'=>423), 200);
		} else {
            $pictureLikes = (array) $pictureLikes;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pictureLikes), 200);
		}
    }
    
    public function post_comment_post()
    {
		$data = new stdClass();
        
        $data->page_id = (int) $this->post('pageID');
        $data->picture_id = (int) $this->post('pictureID');
        $data->picture_comment = $this->post('comment');
        $data->user_id = (int) $this->post('user_id');
        
		if ( empty($data->page_id) ) {
			$this->response(array('error' => 'Required page id','errorCode'=>409), 200);
		}
        
        if ( empty($data->picture_id) ) {
			$this->response(array('error' => 'Required picture id','errorCode'=>411), 200);
		}
        
        if ( empty($data->picture_comment) ) {
			$this->response(array('error' => 'Please provide comment','errorCode'=>412), 200);
		}
		
		$this->load->model('page_model');
		$commentDetails = $this->page_model->post_comment($data);
		
		if ( $commentDetails === FALSE ) {
			$this->response(array('error' => 'Failed to save comment','errorCode'=>413), 200);
		} else {
            $commentDetails = (array) $commentDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$commentDetails), 200);
		}
    }
    
    public function post_like_post()
    {
		$data = new stdClass();
        
        $data->page_id = (int) $this->post('pageID');
        $like_id = (int) $this->post('like_id');
        if ( !empty($like_id) ) {
            $data->like_id = $like_id;
        }
        
        $data->picture_id = (int) $this->post('pictureID');
        $data->like_type = $this->post('like_type');
        $data->user_id = (int) $this->post('user_id');
        
		if ( empty($data->page_id) ) {
			$this->response(array('error' => 'Required page id','errorCode'=>409), 200);
		}
        
        if ( empty($data->picture_id) ) {
			$this->response(array('error' => 'Required picture id','errorCode'=>411), 200);
		}
        
        if ( empty($data->like_type) ) {
			$this->response(array('error' => 'Please provide like type','errorCode'=>414), 200);
		}
		
		$this->load->model('page_model');
		$commentDetails = $this->page_model->post_like($data);
		
		if ( $commentDetails === FALSE ) {
			$this->response(array('error' => 'Failed to save like','errorCode'=>415), 200);
		} else {
            $commentDetails = (array) $commentDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$commentDetails), 200);
		}
    }
    
    public function create_page_post()
    {
		$data = new stdClass();
        
        //$data->school_id = (int) $this->post('school_id');
        $data->page_name = $this->post('page_name');
        $data->page_desc = $this->post('page_desc');
        $data->user_id = (int) $this->post('user_id');
        
		/*if ( empty($data->school_id) ) {
			$this->response(array('error' => 'Required school id','errorCode'=>423), 200);
		}*/
        
        if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
        
        if ( empty($data->page_name) ) {
			$this->response(array('error' => 'Required page name','errorCode'=>424), 200);
		}
        
        if ( empty($data->page_desc) ) {
			$this->response(array('error' => 'Required page description','errorCode'=>426), 200);
		}
		
        $this->load->library('Page_lib');
		$pageDetails = $this->page_lib->create_new_page($data);
        
		if ( $pageDetails === FALSE ) {
			$this->response(array('error' => 'Failed to create new page','errorCode'=>425), 200);
		} else {
            if ( is_array($pageDetails) ) {
                $this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pageDetails), 200);
            } else {
                $this->response(array('error' => 'Page already exists','errorCode'=>427), 200);
            }
		}
    }
    
    public function post_spam_post()
    {
		$data = new stdClass();
        
        $data->spam_id = (int) $this->post('spam_id');
        $data->spam_type = trim($this->post('spam_type'));
        $data->spam_reason = trim($this->post('spam_reason'));
        $data->user_id = (int) $this->post('user_id');
        
		if ( empty($data->spam_id) ) {
			$this->response(array('error' => 'Required spam id','errorCode'=>427), 200);
		}
        
        if ( empty($data->spam_type) ) {
			$this->response(array('error' => 'Required spam type','errorCode'=>428), 200);
		}
        
        if ( empty($data->spam_reason) ) {
			$this->response(array('error' => 'Required spam reason','errorCode'=>429), 200);
		}
        
        if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
        
        if ( !in_array($data->spam_type,array('post','picture','user'))  ) {
			$this->response(array('error' => 'Invalid spam type','errorCode'=>430), 200);
		}
        
        if ( !in_array($data->spam_reason,array('inappropriate','bully','harassment','others'))  ) {
			$this->response(array('error' => 'Invalid spam reason','errorCode'=>431), 200);
		}
		
        $this->load->library('Page_lib');
		$spamDetails = $this->page_lib->post_spam($data);
        
		if ( $spamDetails === FALSE ) {
			$this->response(array('error' => 'Failed to report spam','errorCode'=>432), 200);
		} else {
            $this->response(array('response' => 'Success','responseCode'=>100,'data'=>$spamDetails), 200);
    	}
    }
    
    public function hallway_details_get()
    {
		$data = new stdClass();
        
		$data->show_feeds = FALSE;
		
        $data->academic_id = (int) $this->get('academic_id');
        $data->school_id = (int) $this->get('school_id');
		$data->user_id = (int) $this->get('user_id');
		$data->show_feeds = (boolean) $this->get('show_feeds');
        
		if ( empty($data->academic_id) ) {
			$this->response(array('error' => 'Required academic id','errorCode'=>434), 200);
		}
        
        if ( empty($data->school_id) ) {
			$this->response(array('error' => 'Required school id','errorCode'=>423), 200);
		}
		
		if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
		
		$this->load->library('Page_lib');
		$pageDetails = $this->page_lib->get_hallway_details($data);
		
		if ( $pageDetails === FALSE ) {
			$this->response(array('error' => 'Failed to get hallway data','errorCode'=>435), 200);
		} else {
            $pageDetails = (array) $pageDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pageDetails), 200);
		}
    }

	public function disable_user_post()
    {
		$data = new stdClass();
		
		$data->user_id = $this->post('user_id');
		$data->reason = $this->post('reason');
		$data->is_active = 0;
		
		if ( empty($data->user_id) || empty($data->reason)) {
			$this->response(array('error' => 'Required params user id, reason','errorCode'=>404), 200);
		}
        
        $this->load->library('User_lib');
        $user_info = $this->user_lib->update_user_status($data);
            
        if ( $user_info === FALSE ) {
			$this->response(array('error' => 'Failed to disable user','errorCode'=>401), 200);
		} else {
            if ( $user_info === TRUE ) {
                $this->response(array('response' => 'Success','responseCode'=>100,'data'=>array('user_id'=>$data->user_id)), 200);
            }
        }
		
		$data = null;
	}
        
    public function page_images_get()
    {
		$data->page_id = (int) $this->get('page_id');
		$data->page_name = (string) $this->get('page_name');
        
		if ( empty($data->page_id) && empty($data->page_name)) {
			$this->response(array('error' => 'Required page id or page name', 'errorCode' => 441), 200);
		}
		
        $this->load->library('Page_lib');
		$pageImages = $this->page_lib->get_images_by_page($data);
		
		if ( $pageImages === FALSE ) {
			$this->response(array('error' => 'Failed to get page pictures','errorCode'=>442), 200);
		} else {
            $pageImages = (array) $pageImages;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$pageImages), 200);
		}
    }
    
    public function selfies_get() {
        
        $data = new stdClass();
        
		$data->academic_id = (int) $this->get('academic_id');
        $data->user_id = (int) $this->get('user_id');
        $data->class = (string) $this->get('class');
		
		if ( empty($data->academic_id) ) {
			$this->response(array('error' => 'Required academic id','errorCode'=>434), 200);
		}
        
        if ( empty($data->user_id) ) {
			$this->response(array('error' => 'Required user id','errorCode'=>406), 200);
		}
        
        if ( empty($data->class) ) {
			$this->response(array('error' => 'Required class','errorCode'=>436), 200);
		}
		
		$this->load->library('User_lib');
		$selfiesDetails = $this->user_lib->get_selfies_details($data);
		
		if ( $selfiesDetails === FALSE ) {
			$this->response(array('error' => 'Failed to get selfies data','errorCode'=>435), 200);
		} else {
            $selfiesDetails = (array) $selfiesDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$selfiesDetails), 200);
		}
    }
    
    public function terms_emojies_get() {
        
        $data = new stdClass();
        
		$data->type = strtolower($this->get('type'));
        $data->category = strtolower($this->get('category'));
        $data->random = (boolean) $this->get('random');
        $data->random_terms_count = $this->get('random_terms_count');
		
		if ( empty($data->type) ) {
			$this->response(array('error' => 'Required type','errorCode'=>437), 200);
		}
        
        if ( !empty($data->type) && !in_array($data->type,array('term','emoji')) ) {
			$this->response(array('error' => 'Invalid type','errorCode'=>439), 200);
		}
        
        if ( !empty($data->type) && $data->type == 'term' && empty($data->category) ) {
			$this->response(array('error' => 'Required category','errorCode'=>438), 200);
		}
        
        if ( $data->type == 'emoji' && $data->random == TRUE ) {
            $this->response(array('error' => "Random terms only works with type 'term'",'errorCode'=>442), 200);
        }
        
        if ( $data->type == 'term' && $data->random == TRUE && empty($data->random_terms_count) ) {
            $this->response(array('error' => 'Required random terms count required','errorCode'=>438), 200);
        }
        
        if ( $data->random == FALSE ) {
            $data->random_terms_count = 0;
        }
		
		$this->load->library('term_lib');
		$termDateDetails = $this->term_lib->terms_emojies($data);
		
		if ( $termDateDetails === FALSE ) {
			$this->response(array('error' => 'No term data found','errorCode'=>440), 200);
		} else {
            $termDateDetails = (array) $termDateDetails;
			$this->response(array('response' => 'Success','responseCode'=>100,'data'=>$termDateDetails), 200);
		}
    }
}